package com.yourcompany.web.controller;

import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yourcompany.api.dto.FileQueryDTO;
import com.yourcompany.api.entity.SysFile;
import com.yourcompany.api.service.SysFileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "文件上传管理")
@RestController
@RequestMapping("/api/file")
public class SysFileController {

    @Autowired
    private SysFileService fileService;

    @Operation(summary = "分页查询文件")
    @GetMapping("/page")
    public SaResult pageFiles(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            FileQueryDTO queryDTO) {
        Page<SysFile> page = new Page<>(pageNum, pageSize);
        return SaResult.ok().setData(fileService.pageFiles(page, queryDTO));
    }

    @Operation(summary = "上传文件")
    @PostMapping("/upload")
    public SaResult uploadFile(
            @RequestParam("file") MultipartFile file,
            @RequestParam(required = false) String businessType,
            @RequestParam(required = false) String businessId) {
        return SaResult.ok().setData(fileService.uploadFile(file, businessType, businessId));
    }

    @Operation(summary = "批量上传文件")
    @PostMapping("/upload/batch")
    public SaResult batchUploadFiles(
            @RequestParam("files") MultipartFile[] files,
            @RequestParam(required = false) String businessType,
            @RequestParam(required = false) String businessId) {
        return SaResult.ok().setData(fileService.batchUploadFiles(files, businessType, businessId));
    }

    @Operation(summary = "删除文件")
    @DeleteMapping("/{id}")
    public SaResult deleteFile(@PathVariable Long id) {
        fileService.deleteFile(id);
        return SaResult.ok();
    }

    @Operation(summary = "根据业务删除文件")
    @DeleteMapping("/business")
    public SaResult deleteByBusiness(
            @RequestParam String businessType,
            @RequestParam String businessId) {
        fileService.deleteByBusiness(businessType, businessId);
        return SaResult.ok();
    }
} 